<template>
   <div class="list" ref="wrapper">
    <div>
      <div class="list-block">
        <div class="title border-topbottom">当前城市</div>
        <div class="btn-list">
          <div class="btn-wrapper">
            <button class="btn">{{this.city}}</button>
          </div>
        </div>
      </div>
      <div class="list-block">
        <div class="title border-topbottom">热门城市</div>
        <div class="btn-list">
          <div class="btn-wrapper"
               v-for="item of hotCities"
               :key="item.id"
               @click="handleChangeCity(item.name)">
            <button class="btn">{{ item.name }}</button>
          </div>
        </div>
      </div>
      <div class="list-block"
           v-for="(items, key) of cities"
           :key="key"
           :ref="key"
      >
        <div class="title border-topbottom">{{ key }}</div>
        <ul>
          <li
            class="item border-bottom"
            v-for="item of items"
            :key="item.id"
            @click="handleChangeCity(item.name)"
          >{{ item.name }}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  methods: {
    handleChangeCity (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  computed: {
    ...mapState(['city'])
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper)
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
  .list
    overflow: hidden
    position: absolute
    top: 1.6rem
    left: 0
    right: 0
    bottom: 0
    .title
      height: .54rem
      line-height: .54rem
      background: #eee
      text-indent: .2rem
      font-size: .3rem
    .btn-list
      overflow: hidden
      padding: .1rem .6rem .1rem .1rem
      .btn-wrapper
        width: 33.3%
        float: left
        .btn
          width: 90%
          text-align: center
          background: #fff
          margin: .1rem
          border: .02rem solid #ccc
          border-radius: .06rem
    .item
      height: .54rem
      line-height: .54rem
      text-indent: .2rem
</style>
